package com.yanqu.road.server.manager.activity.yearMaster;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.yearMaster.YearMasterConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.activity.YearMasterBussiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class YearMasterMgr extends TempMgr {
    private static Map<Integer, YearMasterActivity> YearMasterActivityMap = new ConcurrentHashMap<>();


    @Override
    public boolean reloadData() throws Exception {
        loadActivity();
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        for (YearMasterActivity activity : YearMasterActivityMap.values()) {
            activity.save();
        }
        return true;
    }


    /**
     * 区服攻击boss
     *
     * @param userID
     * @param activityId
     * @param score
     * @param gridType
     */
    public static void attackBoss(long userID, int activityId, long score, int gridType, long serverId,int gridNum,int mode,String removeGrid) {
        YearMasterActivity yearMasterActivity = getYearMasterActivity(activityId);
        if (yearMasterActivity != null) {
            yearMasterActivity.attackBoss(userID, score, gridType, serverId,gridNum,mode,removeGrid);
        } else {
            getLogger().error("attackBoss activity is null {} {} {} {} {}" + userID, activityId, score, gridType, serverId);
        }
    }

    /**
     * 区服同步信息
     */
    public static void syncActivity(int activityId) {
        YearMasterActivity yearMasterActivity = getYearMasterActivity(activityId);
        if (yearMasterActivity != null) {
            yearMasterActivity.syncActivityInfo();
        }
    }

//------------------------------------------------------------Activity--------------------------------------------------------------

    public static void loadActivity() {
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.YearMasterActivity.getValue());
        Map<Integer, ActivityInfo> tempActivityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        for (ActivityInfo ac : tempActivityInfoMap.values()) {
            if (!YearMasterActivityMap.containsKey(ac.getActivityId())) {
                synchronized (YearMasterActivityMap) {
                    if (!YearMasterActivityMap.containsKey(ac.getActivityId())) {
                        YearMasterActivityMap.put(ac.getActivityId(), new YearMasterActivity(ac));
                    }
                }
            }

        }
    }

    /**
     * 清除过期活动
     */
    public static void checkExpired() {
        List<Integer> expiredList = new ArrayList<>();
        for (YearMasterActivity activity : YearMasterActivityMap.values()) {
            if ((activity.getActivityInfo().getEndShowTime() + 3 * 24 * 3600) < (System.currentTimeMillis() / 1000)) {
                expiredList.add(activity.getActivityInfo().getActivityId());
            }
        }
        for (int activityId : expiredList) {
            YearMasterActivityMap.remove(activityId);
        }

    }


    public static YearMasterActivity getYearMasterActivity(int activityId) {
        YearMasterActivity yearMasterActivity = YearMasterActivityMap.get(activityId);
        if (yearMasterActivity == null) {
            synchronized (YearMasterActivityMap) {
                if (!YearMasterActivityMap.containsKey(activityId)) {
                    ActivityInfo ac = ActivityBussiness.getActivityInfoByActivityId(activityId);
                    if (ac == null || (ac.getEndShowTime() + 3 * 24 * 3600) < (System.currentTimeMillis() / 1000)) {
                        return null;
                    }
                    if (ac.getType() == eActivityType.YearMasterActivity.getValue()) {
                        YearMasterActivityMap.put(ac.getActivityId(), new YearMasterActivity(ac));
                    }
                }
            }
        }
        return YearMasterActivityMap.get(activityId);
    }

}
